package com.yu8home.mq.rabbit.ttl;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig2 {

    @Bean
    DirectExchange orderTtlDirect() {
        return new DirectExchange(QueueEnum.ORDER_TTL.getExchange());
    }

    @Bean
    DirectExchange orderCancelDirect() {
        return new DirectExchange(QueueEnum.ORDER_CANCEL.getExchange());
    }


    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


    @Bean
    public Queue orderTtlQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-message-ttl", 30 * 1000);
        args.put("x-max-length", 3);
        args.put("x-dead-letter-exchange", QueueEnum.ORDER_CANCEL.getExchange());
        args.put("x-dead-letter-routing-key", QueueEnum.ORDER_CANCEL.getRouteKey());
        return new Queue(QueueEnum.ORDER_TTL.getName(), true, false, false, args);
    }

    @Bean
    public Queue orderCancelQueue() {
        return new Queue(QueueEnum.ORDER_CANCEL.getName());
    }


    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


    @Bean
    Binding orderTtlBinding(DirectExchange orderTtlDirect, Queue orderTtlQueue) {
        return BindingBuilder.bind(orderTtlQueue).to(orderTtlDirect).with(QueueEnum.ORDER_TTL.getRouteKey());
    }

    @Bean
    Binding orderBinding(DirectExchange orderCancelDirect, Queue orderCancelQueue) {
        return BindingBuilder.bind(orderCancelQueue).to(orderCancelDirect).with(QueueEnum.ORDER_CANCEL.getRouteKey());
    }

}
